<template>
  <app-card>
    <template #header>
      <el-input
          v-model="listParams.username"
          placeholder="请输入群ID"
          clearable
          @clear="loadList"
          style="width: 300px"
      >
        <template #append>
          <el-button @click="loadList">
            <el-icon>
              <search/>
            </el-icon>
          </el-button>
        </template>
      </el-input>
      <el-button
          type="danger"
          @click="batchDelete"
      >
        <el-icon>
          <delete/>
        </el-icon>
        批量删除
      </el-button>
    </template>
    <el-table
        :data="list"
        v-loading="listLoading"
        @sort-change="handleSortChange"
        stripe
        style="width: 100%"
    >
      <el-table-column fixed="left" type="selection" width="55"/>
      <el-table-column align="center" :show-overflow-tooltip="true" min-width="120px" prop="groupId" label="群ID"/>
      <el-table-column align="center" min-width="220px" prop="nickName" label="昵称"/>
      <el-table-column align="center" width="110px" prop="avatar" label="头像">
        <template #default="{row}">
          <app-avatar :size="40" :src="row.avatarUrl"></app-avatar>
        </template>
      </el-table-column>
      <el-table-column align="center" width="150px" prop="groupAdmin" label="管理员账号"/>
      <el-table-column align="center" width="170px" prop="createdAt" sortable="custom" label="创建时间">
        <template #default="{row}">
          <el-tag type="info">{{ row.createdAt }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" width="160px" label="操作">
        <template #default="{row}">
          <el-button type="text" @click="showUserList(row)">
            查看用户
          </el-button>
          <el-popconfirm
              title="确认解散该群吗？ 解散后所有聊天记录与文件都被清除！"
              @confirm="dissolutionHandle(row.id)"
          >
            <template #reference>
              <el-button style="color: red" type="text"> 解散</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <app-pagination
        v-model:page="listParams.pageNum"
        v-model:limit="listParams.pageSize"
        :list-count="listCount"
        :load-list="loadList"
        :disabled="listLoading"
    />
    <group-user-list v-model="dialogShow" :item="item"></group-user-list>
  </app-card>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref} from 'vue';
import {IListParams} from "@/api/types/common";
import {dissolution, IGroupInfo, queryGroupList} from "@/api/group";
import GroupUserList from "./components/groupUserList.vue";
import {ElMessage} from "element-plus";

const dialogShow = ref<Boolean>(false)
const item = ref<IGroupInfo>({})
const list = ref<IGroupInfo[]>([]);
const listCount = ref(0);
const listLoading = ref(false);
const listParams = reactive<IListParams>({
  pageNum: 1,
  pageSize: 10,
  username: ''
});

onMounted(() => {
  loadList();
});

const loadList = async () => {
  listLoading.value = true;
  const data = await queryGroupList(listParams).finally(() => {
    listLoading.value = false;
  });
  list.value = data.rows;
  listCount.value = data.total;
};

// 排序
const handleSortChange = ({prop, order}: {
  prop: string; order: string;
}) => {
  listParams.sortField = prop;
  listParams.sortOrder = order
  loadList();
};

// 查看列表
const showUserList = (row: IGroupInfo) => {
  item.value = row
  dialogShow.value = true
}

// 解散群
const dissolutionHandle = async (id: number) => {
  await dissolution(id)
  ElMessage.success('群解散成功！')
  await loadList()
}
</script>

<style lang="scss" scoped>
</style>
